package com.wc.算法基础课.C第三讲搜索与图论.spfa.最优贸易;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/16 9:13
 * @description https://www.acwing.com/problem/content/description/343/
 */
public class Main {
    /**
     * 思路: 如何挣钱, 在路上前面拿最低成本, 后面再贸易出最大成本
     * 如何确定自己拿到的是最低成本呢, 那就保存到每个点最小的成本
     * 那如何确定最大成本呢, 那就每个点存最大成本
     * 怎么保证这个最低和最高是能够到达n点呢, 那我们可以使得最大成本/最小成本能够从n点反向出发
     * 那就保证了可达n了
     * <p>
     * 由于不是最短路的性质, 而是最大值最小值的性质， 因此不不能使用dijkstra, 只能使用spfa
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010, M = 1000010;
    static int[] h = new int[N], rh = new int[N], e = new int[M], ne = new int[M];
    static int[] dmax = new int[N], dmin = new int[N];
    static int INF = 110;
    static boolean[] st = new boolean[N];
    static int idx = 1;
    static int[] w = new int[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) w[i] = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(h, a, b);
            add(rh, b, a);
            if (c == 2) {
                add(h, b, a);
                add(rh, a, b);
            }
        }
        spfa(dmin, 1, true);
        spfa(dmax, n, false);
        int res = 0;
        for (int i = 1; i <= n; i++) res = Math.max(res, dmax[i] - dmin[i]);
        out.println(res);
        out.flush();
    }

    static void spfa(int[] d, int start, boolean flg) {
        if (flg) Arrays.fill(dmin, 1, n + 1, INF);
        d[start] = w[start];
        Queue<Integer> q = new LinkedList<>();
        q.add(start);
        st[start] = true;

        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;
            for (int i = flg ? h[t] : rh[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (flg && d[j] > Math.min(d[t], w[j]) || !flg && d[j] < Math.max(d[t], w[j])) {
                    if (flg) d[j] = Math.min(d[t], w[j]);
                    else d[j] = Math.max(d[t], w[j]);
                    if (!st[j]) {
                        q.add(j);
                        st[j] = true;
                    }
                }
            }
        }
    }

    static void add(int[] h, int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
